package com.hymjweb.demo.hymj.system_maintenance.role_permission.mapper;

import com.hymjweb.demo.hymj.system_maintenance.permission.bean.PermissionPojo;
import com.hymjweb.demo.hymj.system_maintenance.role_permission.bean.RolePermissionPojo;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @BelongsProject: demo
 * @BelongsPackage: com.hymjweb.demo.hymj.system_maintenance.user_role.mapper
 * @Author: luoge
 * @CreateTime: 2021-03-23 18:49
 * @Description:
 */
@Mapper
@Component
public interface RolePermissionMapper {




    class RolePermissionMapperProvider{

        public String getRolePermissionListByRoleId(String roleId){
            String sql = new SQL() {{
                SELECT("t.*");
                FROM("sys_role_permission t ");
                WHERE("1=1 and t.role_id = #{roleId}");
            }}.toString();
            return sql;
        }

        public String getRolePermissionListByRoleIds(String userRoleIds){
            String sql = new SQL() {{
                SELECT("t.*");
                FROM("sys_role_permission t ");
                WHERE("1=1 and t.role_id in ("+ userRoleIds +")");
            }}.toString();
            return sql;
        }

        public String queryRolePermissionByRoleIdAndResourceTypeId(String roleId, Long resourceTypeId){
            String sql = new SQL() {{
                SELECT("t.*");
                FROM("sys_role_permission t ");
                WHERE("1=1 and t.role_id = #{roleId} and t.resource_type_id = #{resourceTypeId}");
            }}.toString();
            return sql;
        }
    }


    @Results(id = "rolePermissionMapId",value = {
            @Result(column = "id", property = "id"),
            @Result(column = "role_id", property = "roleId"),
            @Result(column = "permission_id", property = "permissionId"),
            @Result(column = "resource_type_id", property = "resourceTypeId")
    })
    @SelectProvider(type = RolePermissionMapperProvider.class, method = "getRolePermissionListByRoleId")
    List<RolePermissionPojo> getRolePermissionListByRoleId(String roleId);

    @ResultMap("rolePermissionMapId")
    @SelectProvider(type = RolePermissionMapperProvider.class, method = "getRolePermissionListByRoleIds")
    List<RolePermissionPojo> getRolePermissionListByRoleIds(String userRoleIds);

    @ResultMap("rolePermissionMapId")
    @SelectProvider(type = RolePermissionMapperProvider.class, method = "queryRolePermissionByRoleIdAndResourceTypeId")
    List<RolePermissionPojo> queryRolePermissionByRoleIdAndResourceTypeId(String roleId, Long resourceTypeId);


    @Insert(
            "<script>" +
                    "insert into sys_role_permission(id, role_id, permission_id, resource_type_id) values " +
                    "<foreach collection='rolePermissionPojoList' item='pojo' separator=','>" +
                    "(#{pojo.id},#{pojo.roleId},#{pojo.permissionId},#{pojo.resourceTypeId})" +
                    "</foreach>" +
            "</script>"
    )
    @Options(keyProperty = "id")
    int batchInsert(@Param("rolePermissionPojoList") List<RolePermissionPojo> rolePermissionPojoList);



    @Delete("delete from sys_role_permission t where t.role_id = #{roleId} and t.resource_type_id = #{resourceTypeId}")
    int deleteByRoleIdAndResourceTypeId(String roleId, Long resourceTypeId);

}
